package com.qyhl.webtv.module_news.luckydraw.ticketsinfo;


import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.LuckDrawInfoBean;
import com.qyhl.webtv.module_news.common.NewsUrl;

import java.util.List;

public class LuckDrawTicketsInfoModel implements LuckDrawTicketsInfoContract.LuckDrawTicketsInfoModel {
    private LuckDrawTicketsInfoPresenter mPresenter;

    public LuckDrawTicketsInfoModel(LuckDrawTicketsInfoPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void queryTicketsInfo(String phoneNum, final String lastId) {


        EasyHttp.get(NewsUrl.QUERY_LUCK_DRAW_TICKETS)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("phoneNum", phoneNum)
                .params("lastId", lastId)
                .execute(new SimpleCallBack<List<LuckDrawInfoBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202 ) {
                            if ("0".equals(lastId)) {
                                mPresenter.queryTicketsInfoError(2, "暂无任何发票信息！");
                            } else {
                                mPresenter.queryTicketsInfoError(3, "没有更多发票信息！");
                            }
                        } else {
                            if ("0".equals(lastId)) {
                                mPresenter.queryTicketsInfoError(4, "查询失败！");
                            } else {
                                mPresenter.queryTicketsInfoError(5, "查询失败！");
                            }
                        }
                    }

                    @Override
                    public void onSuccess(List<LuckDrawInfoBean> luckDrawInfoBeans) {
                        if(luckDrawInfoBeans.size() > 0){
                            if ("0".equals(lastId)) {
                                mPresenter.queryTicketsInfoSuccess(luckDrawInfoBeans, false);
                            } else {
                                mPresenter.queryTicketsInfoSuccess(luckDrawInfoBeans, true);
                            }
                        }else{
                            if ("0".equals(lastId)) {
                                mPresenter.queryTicketsInfoError(2, "暂无任何发票信息！");
                            } else {
                                mPresenter.queryTicketsInfoError(3, "没有更多发票信息！");
                            }
                        }
                    }
                });
    }
}
